package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.DateTimeUtils;
import io.renren.common.utils.DateUtils;
import io.renren.modules.app.entity.WithdrawalsRecordingEntity;
import io.renren.modules.app.entity.vo.QueryEarningsParam;
import io.renren.modules.app.entity.vo.SelectEarningsData;
import io.renren.modules.app.entity.vo.SelectWithdrawalsData;
import io.renren.modules.app.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.app.dao.EarningsRecordingDao;
import io.renren.modules.app.entity.EarningsRecordingEntity;
import io.renren.modules.app.service.EarningsRecordingService;


@Service("earningsRecordingService")
public class EarningsRecordingServiceImpl extends ServiceImpl<EarningsRecordingDao, EarningsRecordingEntity> implements EarningsRecordingService {

    @Autowired
    private EarningsRecordingDao earningsRecordingDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EarningsRecordingEntity> page = this.page(
                new Query<EarningsRecordingEntity>().getPage(params),
                new QueryWrapper<EarningsRecordingEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取收益记录
     *
     * @param userIds 用户id 集合
     * @return
     */
    @Override
    public List<EarningsRecordingEntity> getEarningsListByUserIdsgetEarningsRecord(List<Long> userIds) {
        QueryWrapper<EarningsRecordingEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(userIds)) {
            wrapper.in("user_id", userIds);
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 获取用户今日收益金额
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public BigDecimal queryUserTodayIncomeAmount(Long userId) {
        BigDecimal todayCommission = earningsRecordingDao.queryUserTodayIncomeAmount(userId);
        if (todayCommission == null) {
            return new BigDecimal(0.00);
        } else {
            return todayCommission;
        }
    }

    /**
     * 统计用户总收益
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public BigDecimal queryUserTotalRevenue(Long userId) {
        BigDecimal totalRevenue = earningsRecordingDao.queryUserTotalRevenue(userId);
        if (totalRevenue == null) {
            return new BigDecimal(0.00);
        } else {
            return totalRevenue;
        }
    }

    /**
     * 分页获取收益记录
     */
    @Override
    public SelectEarningsData queryEarningsRecording(QueryEarningsParam earningsParam) {
        QueryWrapper<EarningsRecordingEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotNull(earningsParam.getUserId())) {
            wrapper.eq("user_id", earningsParam.getUserId());
        }
        String today = DateTimeUtils.todayDateStr();
        System.out.println(today);
        wrapper.apply("date_format(create_time,'%y%m%dd')=" + "date_format('" + today + "','%y%m%dd')");
        wrapper.orderByDesc("create_time");
        Page<EarningsRecordingEntity> iPage = new Page<>(earningsParam.getPageNum(), earningsParam.getPageSize());
        IPage<EarningsRecordingEntity> IPage = baseMapper.selectPage(iPage, wrapper);
        List<EarningsRecordingEntity> list = IPage.getRecords();

        SelectEarningsData data = new SelectEarningsData();
        data.setTotal(IPage.getTotal());
        data.setCurrentIndex(IPage.getCurrent());
        data.setSize(IPage.getSize());
        data.setPageCount(IPage.getPages());
        data.setEarningsRecordingList(list);
        return data;
    }

    public static void main(String[] args) {

        System.out.println(DateTimeUtils.todayDateStr());
    }

}